<template>
  <div class="app-container">
    <el-form
      ref="passRef"
      :model="temp"
      :rules="rules"
      label-position="left"
      label-width="120px"
      hide-required-asterisk
      style="width: 400px"
    >
      <el-form-item label="旧密码" prop="password">
        <el-input v-model="temp.password" type="password" placeholder="旧密码" />
      </el-form-item>
      <el-form-item label="新密码" prop="newPass">
        <el-input v-model="temp.newPass" type="password" placeholder="新密码" />
      </el-form-item>
      <el-form-item label="确认密码" prop="newPass1">
        <el-input v-model="temp.newPass1" type="password" placeholder="确认密码" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleReset">重置</el-button>
        <el-button type="primary" @click="handleUpdate">修改密码</el-button>
      </el-form-item>
    </el-form>

    <!--图形验证码对话框-->
    <el-dialog title="行为验证" :visible.sync="dialogVerifyVisible" width="370px">
      <verify
        :captcha-type="'blockPuzzle'"
        :mode="'fixed'"
        :img-size="{ width: '330px', height: '155px' }"
        @success="updatePass"
      />
    </el-dialog>
  </div>
</template>

<script>
import Verify from '@/components/verifition/Verify'
import { updatePassword } from '@/api/user'

export default {
  name: 'PassWord',
  components: { Verify },
  data() {
    // 修改密码校验规则
    const editPass1 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else {
        if (this.temp.newPass1 !== '') {
          this.$refs.passRef.validateField('newPass1')
        }
        callback()
      }
    }
    const editPass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新密码'))
      } else if (value !== this.temp.newPass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }

    return {
      temp: {
        password: '',
        newPass: '',
        newPass1: ''
      },
      rules: {
        password: [
          {
            required: true,
            message: '请输入旧密码',
            trigger: 'blur'
          }
        ],
        newPass: [
          {
            required: true,
            validator: editPass1,
            trigger: 'blur'
          }
        ],
        newPass1: [
          {
            required: true,
            validator: editPass2,
            trigger: 'blur'
          }
        ]
      },
      // 验证码对话框
      dialogVerifyVisible: false
    }
  },
  methods: {
    handleUpdate() {
      this.$refs.passRef.validate(valid => {
        if (valid) {
          this.dialogVerifyVisible = true
        }
      })
    },
    // 修改密码
    updatePass() {
      this.dialogVerifyVisible = false
      updatePassword(this.temp).then(res => {
        this.$refs.passRef.resetFields()
        this.$notify({
          title: '成功',
          message: res.msg,
          type: 'success',
          duration: 2000
        })
      }).catch(() => {
        this.$refs.passRef.resetFields()
      })
    },
    handleReset() {
      this.$refs.passRef.resetFields()
    }
  }
}
</script>

<style scoped>

</style>
